package com.example.zxx.shardingsphere.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.zxx.shardingsphere.domain.dto.DateDto;
import com.example.zxx.shardingsphere.domain.dto.TableNameDto;
import com.example.zxx.shardingsphere.domain.entity.User;
import com.example.zxx.shardingsphere.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author zhangxiaoxiang
 * @since 2021-09-14
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户接口")
@Slf4j
@RequiredArgsConstructor
public class UserController {

    public final UserService userService;

    @GetMapping("/list")
    @ApiOperation(value = "用户列表")
    public List<User> list() {
        return userService.list(Wrappers.lambdaQuery(User.class).orderByDesc(User::getCreateTime));
    }


    @PostMapping("/add")
    @ApiOperation(value = "新增用户[主要传创建时间]")
    public String add(@RequestBody DateDto userDto) {
        long l = RandomUtil.randomLong(userDto.getStart(), userDto.getEnd());
        User user1 = User.builder()
                .userName(RandomUtil.randomChar("张周陈李赵王徐") +
                        "" + RandomUtil.randomChar("境楠年劲壤炎夫肃奔硕量帅道诚业峻楠尧坚昆胜栋")
                        + RandomUtil.randomChar("峻卡刊天业淘浩紫涛烨基易越阳绍泉"))
                .password(RandomUtil.randomString(8)).createTime(DateUtil.date(l))
                .phone(RandomUtil.randomLong(18800000000L, 18899999999L) + "").build();
        log.info("新增用户列表:{}", user1);
        try {
            userService.save(user1);
            return "OK";
        } catch (MyBatisSystemException e) {
            log.error("选择表失败,详情:" + e.getMessage(), e);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return "NO";
    }

    @SneakyThrows
    @PostMapping("/add2")
    @ApiOperation(value = "脚本新增用户")
    public String add2(@RequestBody DateDto userDto) {
        log.info("================插入开始...");
        List<User> list = new ArrayList<>();
        for (int i = 0; i < 5 * 10; i++) {
            long l = RandomUtil.randomLong(userDto.getStart(), userDto.getEnd());
            DateTime parse = DateUtil.date(l);
            User user1 = User.builder()
                    .userName(RandomUtil.randomChar("张周陈李赵王徐") +
                            "" + RandomUtil.randomChar("境楠年劲壤炎夫肃奔硕量帅道诚业峻楠尧坚昆胜栋")
                            + RandomUtil.randomChar("峻卡刊天业淘浩紫涛烨基易越阳绍泉"))
                    .password(RandomUtil.randomString(8)).createTime(parse)
                    .phone("188" + RandomUtil.randomInt(10000000, 99999999)).build();
            list.add(user1);
        }
        //每份长度10
        List<List<User>> split = CollUtil.split(list, 10);
        for (List<User> users : split) {
            userService.saveBatch(users);
        }
        return "OK";
    }

    @ApiOperation(value = "创建用户表")
    @PostMapping("/createtable")
    public String createTable(@RequestBody TableNameDto tableNameDto) {
        return userService.createTable(tableNameDto.getTableName());
    }


}
